<?php

namespace app\shopapi\model;

use think\Model;
use app\shopapi\model\ShopBillsStat as ShopBillsStatModel;

class ShopBillsStat extends Model
{
    protected $name = 'shop_bills_stat';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    public function Bills()
    {
        return $this->hasMany('ShopBills', 'stat_id');
    }


    public static function add()
    {
        $shop_list = Shop::where(['audit_status'=>3])->field('shop_id')->select();

        foreach ($shop_list as $k=>$v){
            $exist = ShopBillsStat::get(['shop_id'=>$v['shop_id'],'date'=>date('Y-m-d')]);

            if (!$exist){
                $stat_model = new ShopBillsStat();

                $data = [
                    'shop_id'=>$v['shop_id'],
                    'date'=>date('Y-m-d')
                ];

                $stat_model->save($data);
            }
        }


        return true;
    }


    //获取统计列表
    public function getlist($shop_id)
    {
        $lists = ShopBillsStat::with('Bills')->where(['shop_id'=>$shop_id])
            ->order('createtime desc')->paginate(10);

        return $lists;
    }
}